package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("ProducerDAO")
@Transactional
public class ProducerDAOImpl extends AbstractJpaDao<Producer> implements ProducerDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Producer.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public ProducerDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Producer findProducerById(Integer id) throws DataAccessException {

		return findProducerById(id, -1, -1);
	}

	@Transactional
	public Producer findProducerById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findProducerById", startResult, maxRows, id);
			return (Producer) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Producer> findProducerByName(String name) throws DataAccessException {

		return findProducerByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Producer> findProducerByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findProducerByName", startResult, maxRows, name);
		return new LinkedHashSet<Producer>(query.getResultList());
	}

	@Transactional
	public Set<Producer> findProducerByNameContaining(String name) throws DataAccessException {

		return findProducerByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Producer> findProducerByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findProducerByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Producer>(query.getResultList());
	}

	@Transactional
	public Set<Producer> findProducerByLicense(String license) throws DataAccessException {

		return findProducerByLicense(license, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Producer> findProducerByLicense(String license, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findProducerByLicense", startResult, maxRows, license);
		return new LinkedHashSet<Producer>(query.getResultList());
	}

	@Transactional
	public Set<Producer> findProducerByLicenseContaining(String license) throws DataAccessException {

		return findProducerByLicenseContaining(license, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Producer> findProducerByLicenseContaining(String license, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findProducerByLicenseContaining", startResult, maxRows, license);
		return new LinkedHashSet<Producer>(query.getResultList());
	}

	@Transactional
	public Set<Producer> findProducerByDescription(String description) throws DataAccessException {

		return findProducerByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Producer> findProducerByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findProducerByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Producer>(query.getResultList());
	}

	@Transactional
	public Set<Producer> findProducerByDescriptionContaining(String description) throws DataAccessException {

		return findProducerByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Producer> findProducerByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findProducerByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Producer>(query.getResultList());
	}

	@Transactional
	public Producer findProducerByPrimaryKey(Integer id) throws DataAccessException {
		return findProducerByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Producer findProducerByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findProducerByPrimaryKey", startResult, maxRows, id);
			return (Producer) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Producer> findProducersByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findProducersByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Producer> findAllProducers() throws DataAccessException {

		return findAllProducers(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Producer> findAllProducers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllProducers", startResult, maxRows);
		return new LinkedHashSet<Producer>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Producer> findProducersByAddress(Integer addressId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllProducersByAddress", start, max, addressId);
		return new LinkedHashSet<Producer>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Producer> findProducerByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Producer>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Producer> findProducersByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Producer.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Producer> producers = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return producers;
	}

	@Transactional
	public Producer findProducerByPhysicalPrimaryKey(String name, Integer addressId) throws DataAccessException {
		return findProducerByPhysicalPrimaryKey(name, addressId, -1, -1);
	}

	@Transactional
	public Producer findProducerByPhysicalPrimaryKey(String name, Integer addressId, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findProducerByPhyscialPrimaryKey", startResult, maxRows, name, addressId);
			return (Producer) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Producer entity) {
		return true;
	}
}
